package view;

import model.Article;
import model.User;

import java.util.List;

// 暴力服务器渲染
public class HtmlGenerator {
    public static String getMessagePage(String message, String nextUrl) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<html>");
        stringBuilder.append("<head>");
        stringBuilder.append("<meta charset\"utf-8\">");
        stringBuilder.append("<title>提示页面</title>");
        stringBuilder.append("</head>");
        stringBuilder.append("<body>");

        stringBuilder.append("<h3>");
        stringBuilder.append(message);
        stringBuilder.append("</h3>");

        stringBuilder.append(String.format("<a href=\"%s\"> 点击这里进行跳转 </a>",nextUrl));
        stringBuilder.append("</body>");
        stringBuilder.append("</html>");

        return stringBuilder.toString();
    }

    public static String getArticleListPage(List<Article> articles, User user) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<html>");
        stringBuilder.append("<head>");
        stringBuilder.append("<meta charset\"utf-8\">");
        stringBuilder.append("<title>提示页面</title>");
        stringBuilder.append("</head>");
        stringBuilder.append("<body>");
        stringBuilder.append("<div> 欢迎您! " + user.getName() + "</div>");

        // 增加一个发布文章的页面 ;
        stringBuilder.append("<div> 发布文章 </div>");
        stringBuilder.append("<div>");
        stringBuilder.append("<form method=\"post\" action=\"article\">");
        stringBuilder.append("<h3><input type=\"text\" name=\"title\" placeholder=\"请输入标题\"></h3>");
        stringBuilder.append("<br>");
        stringBuilder.append("<textarea name=\"content\" style=\"width: 500px; height: 300px;\"> </textarea>");
        stringBuilder.append("<br>");
        stringBuilder.append("<input type=\"submit\" value=\"发布博客\">");
        stringBuilder.append("</form>");
        stringBuilder.append("</div>");

        // 要有一个文章列表,显示文章标题
        for(Article article : articles){
            // 在这里 构造 get 请求, 加上 articleId=index;
            stringBuilder.append(String.format("<div> <a href=\"article?articleId=%d\"> %s </a>  <a href=\"deleteServlet?articleId=%d\"> 删除 </a> </div>",article.getArticleId(),article.getTitle(),article.getArticleId()));
        }
        stringBuilder.append(String.format("<div> 当前共有博客 %d 篇",articles.size()));
        stringBuilder.append("</body>");
        stringBuilder.append("</html>");
        return stringBuilder.toString();
    }

    // 显示详情
    public static String getArticleDetailPage(Article article, User user, User author) {

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<html>");
        stringBuilder.append("<head>");
        stringBuilder.append("<meta charset\"utf-8\">");
        stringBuilder.append("<title>提示页面</title>");
        stringBuilder.append("</head>");
        stringBuilder.append("<body>");
        stringBuilder.append("<div> 欢迎您! " + user.getName() + "</div>");
        stringBuilder.append(String.format("<h1> %s </h1>",article.getTitle()));
        stringBuilder.append(String.format("<h3> 作者 : %s </h1>",author.getName()));
        // 将/n 替换成 <br/>

        stringBuilder.append(String.format("<div>%s</div>",article.getContent().replace("\n","<br/>")));

        stringBuilder.append("</body>");
        stringBuilder.append("</html>");
        return stringBuilder.toString();
    }
}
